<?php


namespace App\Log;


use App\Models\Admin\AdminRegion;
use App\Models\Admin\Company;
use App\Models\Admin\CompanyType;
use App\Models\Admin\Document;
use Auth,Saas;
use Illuminate\Support\Arr;

class DocumentLog extends AdminLog
{
    protected $pre = [];    //修改前
    protected $next = [];   //修改后
    protected $module;      //操作模块

    protected $mean = [
        'first_menu_id' => '一级菜单id',
        'sec_menu_id' => '二级菜单id',
        'title' => '标题',
        'author' => '作者',
        'content' => '手册内容',
        'status' => [1=>'已发布',0=>'未发布'],
    ];

    protected $other = [    //数组对应字段
        'status' => '发布状态',
        'type' => '类型',
    ];

    protected $document_type_list=[
            '1'=>'客户手册',
            '2'=>'常见问题',
            '3'=>'更新记录',
            '4'=>'站内通知',
        ];

    public function __construct($pre,$next,$module)
    {
        $this->pre = $pre;
        $this->next = $next;
        $this->module = $module;
    }

    public function addLog()
    {
        $this->addMean();
        $this->data = $this->combinedData();
        parent::addLog();
    }

    //将其他数据组合
    protected function addMean():void
    {
        $type = Arr::pluck(CompanyType::get()->toArray(),'name','id');

        Arr::set( $this->mean,'type',$type);
    }

    public function combinedData():array
    {
        $content = [];
        if(isset($this->pre['id']))
        {
            $document_type=Document::where('id',$this->pre['id'])->value('document_type');
            if($document_type)
            {
                $content['操作对象']=$this->document_type_list[$document_type];
            }
        }
        if (!empty( $this->next)){
            foreach ($this->next as $key => $val){
                if (isset( $this->mean[$key])){
                    if (isset( $this->other[$key])){
                        $meanInfo = "'". $this->other[$key]."'";
                    }else{
                        $meanInfo = "'". $this->mean[$key]."'";
                    }
                    if (is_array($this->mean[$key])){
                        $content[$meanInfo] =  $this->mean[$key][$this->pre[$key]].' -> '. $this->mean[$key][$val];
                    }else{
                        if ($key == 'content'){
                            $content[$meanInfo] = '修改手册内容';
                        }else{
                            $content[$meanInfo] =  $this->pre[$key].' -> '. $this->next[$key];
                        }
                    }
                }
            }
        }
        $data = [
            'module' =>  $this->module->pid,
            'operate' =>  $this->module->id,
            'type' =>  $this->module->type,
            'main' =>  isset($this->next['title']) ? $this->next['title'] : $this->pre['title'],
            'main_id' => isset($this->pre['id']) ? $this->pre['id'] : 0,
            'content' => json_encode($content,JSON_UNESCAPED_UNICODE),
            'user_id' => auth('admin')->user()->id,
            'ip'    => get_proxy_ip(),
            'model' => $this->module->model,
        ];

        return $data;
    }
}
